from ..exts import db
from ..model import BaseModel

""" 多对多中间表 """
article_tag = db.Table(
    'third_article_tags',
    db.Column('article_id', db.Integer, db.ForeignKey('tb_article.id'), primary_key=True),
    db.Column('tag_id', db.Integer, db.ForeignKey('tb_tag.id'), primary_key=True)
)

note_tag = db.Table(
    'third_note_tags',
    db.Column('note_id', db.Integer, db.ForeignKey('tb_note.id'), primary_key=True),
    db.Column('tag_id', db.Integer, db.ForeignKey('tb_tag.id'), primary_key=True)
)


class Article(BaseModel):  # 文章模型
    __tablename__ = 'tb_article'
    title = db.Column(db.String(256), comment='标题')
    content_md = db.Column(db.Text, comment='markdown源内容')
    content_html = db.Column(db.Text, comment='html内容')
    read_count = db.Column(db.Integer, default=0, comment='阅读次数')
    draft_flag = db.Column(db.Boolean, default=False, comment='是否为草稿')

    category_id = db.Column(db.Integer, db.ForeignKey('tb_category.id'), comment='分类id')
    user_id = db.Column(db.Integer, db.ForeignKey('tb_user.id'), comment='用户id')

    tags = db.relationship('Tag', secondary=article_tag, backref='articles')
    '''文章取标签 通过 tags类属性 , 标签取文章通过 backref='articles',
     他俩通过secondary=article_tag关联
    多对多关系只需在一方定义relationship()即可'''

    comments = db.relationship('Comment', backref='article', cascade='delete')
    '''Comment 通过backref='article' 取文章 because一个评论只能取一个文章
    
    '''
    attachments = db.relationship('Attachment', backref='article')

    def __str__(self):
        return self.title


class Category(BaseModel):  # 分类表
    __tablename__ = 'tb_category'
    name = db.Column(db.String(64), unique=True, comment='分类名')

    articles = db.relationship('Article', backref='category')
    notes = db.relationship('Note', backref='category')


class Tag(BaseModel):  # 标签表
    __tablename__ = 'tb_tag'
    name = db.Column(db.String(64), unique=True, comment='标签名')
    color = db.Column(db.String(32), comment='标签颜色')


class Note(BaseModel):  # 笔记模型
    __tablename__ = 'tb_note'
    title = db.Column(db.String(256), comment='标题')
    content_md = db.Column(db.Text, comment='markdown源内容')
    content_html = db.Column(db.Text, comment='html内容')
    read_count = db.Column(db.Integer, default=0, comment='阅读次数')

    category_id = db.Column(db.Integer, db.ForeignKey('tb_category.id'), comment='分类id')
    user_id = db.Column(db.Integer, db.ForeignKey('tb_user.id'), comment='用户id')

    tags = db.relationship('Tag', secondary=note_tag, backref='note')
    attachments = db.relationship('Attachment', backref='note')


class Comment(BaseModel):  # 评论表
    __tablename__ = 'tb_comment'
    content = db.Column(db.Text, comment='评论内容')
    visitor_name = db.Column(db.String(64), comment='游客名')
    visitor_email = db.Column(db.String(64), comment='游客邮箱')
    visitor_ip = db.Column(db.String(64), comment='游客ip')
    visitor_address = db.Column(db.String(64), comment='游客地址')

    article_id = db.Column(db.Integer, db.ForeignKey('tb_article.id'), comment='关联文章')

    replied_id = db.Column(db.Integer, db.ForeignKey('tb_comment.id'), comment='回复的评论id')

    replied = db.relationship('Comment', backref='replies', remote_side='Comment.id', cascade='delete')


class Message(BaseModel):  # 留言表
    __tablename__ = 'tb_message'
    content = db.Column(db.Text, comment='留言内容')
    visitor_name = db.Column(db.String(64), comment='游客名')
    visitor_email = db.Column(db.String(64), comment='游客邮箱')
    visitor_ip = db.Column(db.String(64), comment='游客ip')
    visitor_address = db.Column(db.String(64), comment='游客地址')


class Attachment(BaseModel):  # 附件表
    __tablename__ = 'tb_attachment'
    file_name = db.Column(db.String(256), comment='文件名')
    file_size = db.Column(db.String(32), comment='文件大小')
    file_path = db.Column(db.String(256), comment='文件路径')

    article_id = db.Column(db.Integer, db.ForeignKey('tb_article.id'), comment='关联文章')
    note_id = db.Column(db.Integer, db.ForeignKey('tb_note.id'), comment='笔记id')
